//
//  UserViewController.m
//  ErWa
//
//  Created by 王阳阳 on 16/3/10.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "UserViewController.h"
#import "StrategyTableviewCell.h"
#import "StrategyModel.h"
#import "StratrgyDetailViewController.h"
#import "ChoiceTableviewCell.h"
#import "ChoiceModel.h"
#import "NewNestHeader.h"
#import "SubjectCell.h"
#import "SubjectModel.h"
#import "SubjectDetailPageVC.h"
#import "ChooseDetailViewController.h"
#import "SubjectDetailPageModel.h"
@interface UserViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *arr;/**< 用于存放收藏内容的数组 */
@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = self.str;
    [self dataHandler];//数据处理
    [self createSubviews];//Tableview
}

#pragma mark - /// 数据处理
- (void)dataHandler
{
    //获取文件夹的路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:_judgeStr];
    //获取当前用户名
    NSString *nameStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    NSString *namePath = [path stringByAppendingPathComponent:nameStr];
    // 判断是哪个的收藏
    if ([_judgeStr isEqualToString:@"Boutique"]) {
        //精选
        NSString *detailPath = [namePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", _judgeStr]];
        //获取数组
        self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:detailPath];
    }else {
        //攻略和专题
        NSString *detailPath = [namePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@Modal.plist", _judgeStr]];
        //获取数组
        self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:detailPath];
    }
    
    
}


#pragma mark - /// 初始化视图
- (void)createSubviews
{
    // 设置编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.tableview = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    _tableview.backgroundColor = [UIColor clearColor];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    // 分别注册三种cell
    [_tableview registerClass:[StrategyTableviewCell class] forCellReuseIdentifier:@"strategyCell"];
    [_tableview registerClass:[ChoiceTableviewCell class] forCellReuseIdentifier:@"boutiqueCell"];
    // 如果当前的数组里面没有数据, 就显示当前没有收藏
    if (self.arr.count == 0) {
        //背景图片
        //显示当前没有收藏
        UILabel *tips = [UILabel new];
        [self.view addSubview:tips];
        tips.frame = CGRectMake(self.view.frame.size.width / 4, self.view.frame.size.height / 5 * 3, self.view.frame.size.width / 2, self.view.frame.size.height / 5);
        tips.text = @"您还没有任何收藏哦~快去收藏吧❤️❤️";
        tips.textAlignment = NSTextAlignmentCenter;
        tips.numberOfLines = 0;
        tips.font = [UIFont systemFontOfSize:20];
    }
    
}
#pragma mark - Tableview必须实现的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([_judgeStr isEqualToString:@"Boutique"]) {
        NSLog(@"精选");
        
        ChoiceTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"boutiqueCell" forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.model = _arr[indexPath.row];
        // 判断当前是什么模式
        BOOL model = [AccountManager sharedInstance].isNight;
        if (model == YES) {
            cell.backgroundColor = [UIColor grayColor];
            self.view.backgroundColor = [UIColor grayColor];
        } else {
            cell.backgroundColor = [UIColor clearColor];
        }
        return cell;
    }
    else if ([self.judgeStr isEqualToString:@"Strategy"]) {
        StrategyTableviewCell *cell = [_tableview dequeueReusableCellWithIdentifier:@"strategyCell" forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.model = _arr[indexPath.row];
        // 判断当前是什么模式
        BOOL model = [AccountManager sharedInstance].isNight;
        if (model == YES) {
            cell.backgroundColor = [UIColor grayColor];
            self.view.backgroundColor = [UIColor grayColor];
        } else {
            cell.backgroundColor = [UIColor clearColor];
            
        }
        return cell;
          }
    return nil;
}
#pragma mark - // 设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_judgeStr isEqualToString:@"Boutique"]) {
        return [UIScreen mainScreen].bounds.size.height / 3;;
    } else   {
        return [UIScreen mainScreen].bounds.size.height / 3.5;
    }
}
#pragma mark - Tableview点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //攻略点击详情页面
    if ([_judgeStr isEqualToString:@"Strategy"]) {
        StratrgyDetailViewController *stratrgy = [[StratrgyDetailViewController alloc]init];
        StrategyModel *m = _arr[indexPath.row];
        stratrgy.idStr = m.detailId;
        stratrgy.mainModal = m;
        [self.navigationController pushViewController:stratrgy animated:YES];
    }
    else if ([_judgeStr isEqualToString:@"Boutique"]) {
        ChooseDetailViewController *boutiqueVC = [ChooseDetailViewController new];
        ChoiceModel *m = _arr[indexPath.row];
        boutiqueVC.modal = m;
        boutiqueVC.navigationItem.title = @"精选详情";
        [self.navigationController pushViewController:boutiqueVC animated:YES];
    }
}
#pragma mark - /// 是否编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark - /// 确定cell的编辑样式(删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - /// 编辑按钮的触发
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableview setEditing:editing animated:YES];
}

#pragma mark - 删除
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

#pragma mark - /// 通过编辑状态判断编辑操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 如果风格是删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //获取文件夹的路径
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:_judgeStr];
        // 判断是哪个的收藏
            NSString *detailModalPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@Modal.plist", _judgeStr]];
            // 将所选的modal移除
            [_arr removeObjectAtIndex:indexPath.row];
            // 将新的数组存到本地
            [NSKeyedArchiver archiveRootObject:_arr toFile:detailModalPath];
            // tableview视图同步
            [_tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            if (_arr.count == 0) {
                UIImageView *backImg = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
                [self.view addSubview:backImg];
                backImg.image = [UIImage imageNamed:@"collectionBack.jpg"];
                //显示当前没有收藏
                UILabel *tips = [UILabel new];
                [self.view addSubview:tips];
                tips.frame = CGRectMake(self.view.frame.size.width / 4, self.view.frame.size.height / 5 * 3, self.view.frame.size.width / 2, self.view.frame.size.height / 5);
                tips.text = @"您还没有任何收藏哦~快去收藏吧❤️❤️";
                tips.textAlignment = NSTextAlignmentCenter;
                tips.numberOfLines = 0;
                tips.font = [UIFont systemFontOfSize:20];
                
            }
            
        }
        
    }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
